import {observable, action, toJS} from 'mobx';
// import * as types from '../constants/ActionTypes'
import * as API_LIST from '../../apis/Maint/MeetingRoom';
import isEmpty from 'lodash/isEmpty';
import objectAssign from 'object-assign';
import {WeaTableNew,WeaForm} from 'comsMobx';
const {TableStore} = WeaTableNew;

export default class MeetingRoom {
	@observable dataKey = "";
	@observable selectedKey=0;
	@observable tableStore = new TableStore();
	@observable shareDataKey = '';
	@observable shareTable = new TableStore();
	@observable topTab = [];
	//用于控制高级搜索的显隐的变量
	@observable ifShowSearchAd = false;
	@observable adSearchForm = new WeaForm();
	@observable roomInfoForm = new WeaForm();
	@observable addShareForm = new WeaForm();
	@observable conditioninfo = [];
	@observable showLocked = false;
	@observable showCreate = false;
	@observable spinning = false;
	@observable showDialogFlag = false;
	@observable showAddShareFlag = false;
	@observable roomid = '';
	@observable fields = [];
	@observable shareFields = [];
	@observable viewType = 'create';
	@observable showTab = 'info';
	doLoading = loading => {
		this.loading = loading;
	}

	@action setSelectedKey(newKey) {
        this.selectedKey = newKey;
    }

	getRoomList = (value = {}) => {
		this.loading = true;
		if (value.statuss == 2){
			this.showLocked = true;
		}else{
			this.showLocked = false;
		}
		//调用API
		API_LIST.getRoomList(value).then(action((data) => {
			this.tableStore.getDatas(data.sessionkey, 1);
			this.loading = false;
			this.dataKey = data.sessionkey;
		}));
		if (value.statuss){
			this.setSelectedKey(value.statuss);
		}
	}

	setShowSearchAd(bool) {
		this.ifShowSearchAd = bool;
	}

	initCondition = params => {
		API_LIST.getCondition(params).then(action((data) => {
			const {condition, topTab } = data;
			this.adSearchForm.initFormFields(condition);
			this.topTab = topTab;
			this.conditioninfo = condition;
		}));
	}

	setRoomId = value =>{
		this.roomid = value;
		
	}
	showDialog = value => {
		if(value){//有id 
			this.setRoomId(value);
			this.getShareList({ id: value, preview: this.viewType == 'edit'?0:1 });
		}else{
			this.setRoomId('');
		}
		this.showDialogFlag = true;
	}

	closeDialog = (value = {}) => {
		this.showDialogFlag = false;
	}

	changeViewType = value =>{
		this.viewType = value;
	}

	changeTab = value =>{
		this.showTab = value;
	}
	doSave = (value = {}) =>{
		if (this.roomid==''){
			return API_LIST.add(value);
		}else{
			return API_LIST.edit({...value,roomid:this.roomid});
		}
		
	}

	del = (value = {}) => {
		return API_LIST.del(value);
	}

	lock = (value = {})=> {
		return API_LIST.lock(value);
	}

	unlock = (value = {}) => {
		return API_LIST.unlock(value);
	}

	getFields = (value = {}) =>{
		return API_LIST.getFields(value).then(action((data)=>{
			const {fields} = data;
			this.roomInfoForm.initFormFields(fields);
			this.fields=fields;
		}));
	}

	getShareList = (value = {}) => {
		this.loading = true;
		//调用API
		API_LIST.getShareList(value).then(action((data) => {
			this.shareTable.getDatas(data.sessionkey, 1);
			this.loading = false;
			this.shareDataKey = data.sessionkey;
		}));
	}

	getRoomShareFields = (value = {})=>{
		return API_LIST.getRoomShareFields(value).then(action((data) => {
			const {fields} = data;
			this.shareFields = fields;
		}));
	}

	showShare = (value = {})=>{
        this.addShareForm.reset();
		this.showAddShareFlag = true;
	}

	closeShare = (value = {}) =>{
		this.showAddShareFlag=false;
	}

	saveShare = (value = {}) => {
		return API_LIST.saveShare({...value,id:this.roomid}).then(action((data) => {
			this.getShareList({ id: this.roomid, preview: 0 });
			this.closeShare();
		}));
	}

	deleteShare = (value={}) =>{
		return API_LIST.delShare(value).then(action((data) => {
				this.getShareList({ id: this.roomid, preview: 0 });
			}));
	}
}
